package com.hpeng.sys.dao.impl;

import java.util.List;
import org.springframework.stereotype.Component;
import com.hpeng.sys.dao.SysUserDao;
import com.hpeng.sys.model.Department;
import com.hpeng.sys.model.Staff;
import com.hpeng.sys.model.SysTemplate;
import com.hpeng.sys.model.SysUser;
import com.hpeng.util.GenericDaoImpl;
import com.hpeng.util.Pagination;

@Component("sysUserDao")
public class SysUserDaoImpl extends GenericDaoImpl<SysUser, String>
implements   SysUserDao {

	public List<SysUser> getSysUsers() {
		return getAll();
	}
	
	public SysUser getUser(String userCode){
		System.out.println(userCode+"asdasdasdasdasdasd");
		return get(userCode);
	}
	
	public void saveUser(SysUser sysUser){
		System.out.println(sysUser.getUserCode()+"dddddddddddddddddddddddddddddddddddddddd");
		this.save(sysUser);
	}
	
	public List<SysUser> getUserByCode(String usercode){
		System.out.println(usercode);
		List<SysUser> sysUsers = null;
			sysUsers = getByHql("from SysUser user where user.userCode='"+usercode+"'");
			System.out.println(sysUsers.get(0).getUserCode());
		return sysUsers;
	}
	
	public List<SysUser> getUserByName(String username){
		System.out.println(username);
		List<SysUser> sysUsers = null;
			sysUsers = getByHql("from SysUser user where user.userName like '%"+username+"%'");
			System.out.println(sysUsers.get(0).getUserCode());
		return sysUsers;
	}
	
	public List<SysUser> getUserByTpl(List<SysTemplate> Tpl){
		System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww");
		System.out.println(Tpl.size());
		String queryString = "from SysUser user where user.templateCode='"+Tpl.get(0).getTemplateCode()+"'";
		if(Tpl.size()>1){
			for(int i = 1;i < Tpl.size();i++){
				queryString += " or user.templateCode='"+Tpl.get(i).getTemplateCode()+"'";
			}
		}
		System.out.println(queryString);
		List<SysUser> sysUsers = null;
			sysUsers = getByHql(queryString);
		return sysUsers;
	}
	
	public List<SysUser> getUserByStaff(List<Staff> staffs){
		String queryString = "from SysUser user where user.staid='"+staffs.get(0).getStaid()+"'";
		if(staffs.size()>1){
			for(int i = 1;i < staffs.size();i++){
				queryString += " or user.staid='"+staffs.get(i).getStaid()+"'";
			}
		}
		List<SysUser> sysUsers = null;
			sysUsers = getByHql(queryString);
		return sysUsers;
	}

	public List<SysUser> getUserByDep(List<Department> departments){
		String queryString = "from SysUser user where user.deptCode='"+departments.get(0).getDepid()+"'";
		if(departments.size()>1){
			for(int i = 1;i < departments.size();i++){
				queryString += " or user.deptCode='"+departments.get(i).getDepid()+"'";
			}
		}
		List<SysUser> sysUsers = null;
			sysUsers = getByHql(queryString);
			return sysUsers;
	}
	public void deleteUser(List<String> deleteUsers){
		for(int i =0;i<deleteUsers.size();i++){
			remove(deleteUsers.get(i));
		}
	}
	public Pagination<SysUser> getPagination(int pageNumber) {
		return getPagination("from SysUser order by seq asc", null,
				pageNumber);
	}
	public void updateUser(SysUser sysUser){
		update(sysUser);
	}

}